package com.infore.airsupervise.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.infore.airsupervise.R;
import com.infore.airsupervise.bean.EventE;
import com.infore.airsupervise.bean.UserE;

import java.util.List;

/**
 * Created by Administrator on 2017/8/17.
 */

public class EventAdapter extends RecyclerView.Adapter<EventAdapter.ViewHolder> {

    private List<EventE> list;
    private Context context;
    private int flag;

    public EventAdapter(Context context, List<EventE> list, int flag) {
        this.list = list;
        this.context = context;
        this.flag = flag;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public int getItemViewType(int position) {
        EventE item = list.get(position);
        if (flag == 1) {
            if (!item.getFlag().equals("") && item.getFlag().equals("2")) {
                return 2;
            }
        } else if (flag == 2) {
            if (UserE.getUserInfo().getUserId().equals(item.getReporterId())) {
                if (!item.getFlag().equals("") && item.getFlag().equals("2")) {
                    return 2;
                }
            } else {
                return 2;
            }
        }
        return 1;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_event_item, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.title.setText(list.get(position).getEventName());


        holder.modify.setImageResource(R.drawable.event_modify);
        holder.desc.setText(list.get(position).getDescriptions());
        holder.time.setText(list.get(position).getEventTime());

        if (list.get(position).getFlag().equals("0")) {
            holder.handler.setText("未处理");
            holder.handler.setTextColor(context.getResources().getColor(R.color.air_level_5));
            holder.modify.setImageResource(R.drawable.event_modify);

        } else if (list.get(position).getFlag().equals("1")) {
            holder.handler.setText("处理中");
            holder.handler.setTextColor(context.getResources().getColor(R.color.air_level_1));
            holder.modify.setImageResource(R.drawable.event_modify);
        } else {
            holder.handler.setText("已完成");
            holder.handler.setTextColor(context.getResources().getColor(R.color.air_level_0));
            holder.modify.setVisibility(View.GONE);
        }

    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView title;
        ImageView modify;
        TextView desc;
        TextView handler;
        TextView time;

        public ViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.event_item_title);
            modify = (ImageView) itemView.findViewById(R.id.event_item_modify);
            desc = (TextView) itemView.findViewById(R.id.event_item_desc);
            handler = (TextView) itemView.findViewById(R.id.event_item_handler);
            time = (TextView) itemView.findViewById(R.id.event_item_time);

        }

    }
}
